<?
	class keywords {
	  var $fields;
	  
	  function keywords($id="")  {
	    global $conn;
	    $this->fields = array();
	   if($id!="")  {
		$qry = "SELECT * FROM keywords WHERE id=$id;";
		$rs = $conn->Execute($qry);
		if($rs->RecordCount() > 0) {
		  $fields = $rs->FieldCount();
		  for($i=0;$i<$fields;$i++)  {
		    $field = $rs->FetchField($i);
			$this->fields[$field->name] = $rs->Fields($field->name);
		  }
		} else 
		   $this->fields = NULL;
	    } else	
			$this->fields = NULL;	 
	  }
	  
	  function save($post)  {
		global $conn,$lang, $ftp_host,$ftp_username, $ftp_pwd, $path_key_ita,$path_key_ita_ftp,$path_template, $path_key_eng,$path_key_eng_ftp, $path_key_piscine,$path_key_piscine_ftp, $path_key_giardino,$path_key_giardino_ftp, $path_key_casa,$path_key_casa_ftp, $path_key_fitness,$path_key_fitness_ftp;
		$qry_fields = "";
############################################################################################
	    	$arrsave = array();
	    	$qry_fields = "";
	    	$arrfields = $conn->MetaColumnNames("keywords");
###################### DEBUG #####################################################
	    	/*
			foreach($arrfields as $field => $value) {
			  $qry_fields .= "$value<br>";
			}
	    	foreach($post as $field => $value) {
			  $qry_fields .= "$field=$value<br>";
			}
			die($qry_fields);
			*/
##################################################################################
	    	foreach($post as $field => $value)  {
			  if(in_array($field,$arrfields)) {
			    $arrsave[$field] = $value;
				$qry_fields .= ",'$value'";    
			  }
			}
		$qry = "INSERT INTO keywords VALUES(NULL $qry_fields,'$lang','".$post['sezionec']."','y');";
		//die($qry);
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $error = 0;
		  $rs->Close();
		  $path = ${"path_key_".$post['sezionec']};
		  $path_ftp = ${"path_key_".$post['sezionec']."_ftp"};
		  $folder = eregi_replace(" ","-",$post['titolo']);
		  $connect = ftp_connect($ftp_host);
		  $login = ftp_login($connect, $ftp_username, $ftp_pwd);
		  if(ftp_mkdir($connect,$path_ftp.$folder))  {
		    ftp_site($connect, "CHMOD 0777 ".$path_ftp.$folder);
			@copy($path_template,$path.$folder."/index.php");
			//@copy("includes/config.inc.php",$path.$folder."/config.inc.php");
			//@copy("includes/keywords.class.php",$path.$folder."/keywords.class.php");	
		  }
		} else
		  $error = "Errore nell'aggiornamento.";
		return $error;
	  }
	  
	  function update($post)  {
	    global $conn,$lang, $ftp_host,$ftp_username, $ftp_pwd, $path_key_ita,$path_key_ita_ftp,$path_template, $path_key_eng,$path_key_eng_ftp, $path_key_piscine,$path_key_piscine_ftp, $path_key_giardino,$path_key_giardino_ftp, $path_key_casa,$path_key_casa_ftp, $path_key_fitness,$path_key_fitness_ftp;
		$titolo = $this->fields['titolo'];
		foreach($this->fields as $field => $value) {
		  $this->fields[$field] = $post[$field];
		 if($field!="lang" && $field!="attivo")  {
			if($field == "titolo")
		      $qry_fields = "$field='".$this->fields[$field]."'";
		  else
		    $qry_fields .= ",$field='".$this->fields[$field]."'"; 
		 }		   
		}
		$qry = "UPDATE keywords SET $qry_fields WHERE id=".$this->fields['id'].";";
		//die($qry);
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $error = "Keyword aggiornata con successo!";
		  if($post['titolo'] != $titolo)  {
			$path = ${"path_key_".$post['sezione']};
		    $path_ftp = ${"path_key_".$post['sezione']."_ftp"};
		    $newfolder = eregi_replace(" ","-",$post['titolo']);
		    $oldfolder = eregi_replace(" ","-",$titolo);
		    $connect = ftp_connect($ftp_host);
		    $login = ftp_login($connect, $ftp_username, $ftp_pwd);
		    //die($path_ftp.$oldfolder."<br>".$path_ftp.$newfolder);
		    if(ftp_rename($connect,$path_ftp.$oldfolder,$path_ftp.$newfolder))  {}
		  }
		  $rs->Close();
		} else
		  $error = "Errore nell'aggiornamento.";
		return $error;  	      
	  }
	  
	  function delete()  {
	    global $conn,$lang, $ftp_host,$ftp_username, $ftp_pwd, $path_key_ita,$path_key_ita_ftp,$path_template, $path_key_eng,$path_key_eng_ftp, $path_key_piscine,$path_key_piscine_ftp, $path_key_giardino,$path_key_giardino_ftp, $path_key_casa,$path_key_casa_ftp, $path_key_fitness,$path_key_fitness_ftp;
			$qry = "DELETE FROM keywords WHERE id=".$this->fields['id'].";";
			$rs = $conn->Execute($qry);
			if($rs)  {
		  	  $error = 0;
		  	  $rs->Close();
		  	  $path = ${"path_key_".$this->fields['sezione']};
		  	  $path_ftp = ${"path_key_".$this->fields['sezione']."_ftp"};
		  	  $folder = eregi_replace(" ","-",$this->fields['titolo']);
		  	  $connect = ftp_connect($ftp_host);
		  	  $login = ftp_login($connect, $ftp_username, $ftp_pwd);
		  	  $files = ftp_nlist($connect,$path_ftp.$folder);
		  	  if(is_array($files) && count($files) > 0)  {
		   		foreach($files as $chiave => $nome)  { 
		     	  @ftp_delete($connect, $nome);
		   	  	}
		 	  }  
		     ftp_rmdir($connect, $path_ftp.$folder);
			} else
		  	    $error = "Errore nell'aggiornamento.";
			return $error;
	}
	
	function disactive()  {
	    global $conn;
		$qry = "UPDATE keywords SET attivo='n' WHERE id=".$this->fields['id'].";";  
	    $rs = $conn->Execute($qry);
	        if($rs) {
	          $error = "0";
	        }  else  {
	          $error = "Errore nell'operazione.";
	        }
	         $rs->Close();
	       return $error;        
	  }
  
	  function active()  {
	    global $conn;
		$qry = "UPDATE keywords SET attivo='y' WHERE id=".$this->fields['id'].";";  
	    $rs = $conn->Execute($qry);
	        if($rs) {
	          $error = "0";
	        }  else  {
	          $error = "Errore nell'operazione.";
	        }
	         $rs->Close();
	       return $error;            
	  }
	  
	  function getMeta($lang)  {
		global $conn;
		$qry = "SELECT titolo FROM keywords WHERE lang='$lang' AND attivo='y';";
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $meta = '';
		  while(!$rs->EOF)  {
		  	$meta .= ' '.$rs->Fields("titolo");
		    $rs->MoveNext();	
		  }
		  $rs->Close();
		}
		return $meta;
	}
	
	  function getKeyMenu($lang)  {
		global $conn;
		$qry = "SELECT id,titolo FROM keywords WHERE lang='$lang' AND attivo='y';";
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $menu = '';
		  while(!$rs->EOF)  {
		  	$folder = eregi_replace(" ","-",$rs->Fields("titolo"));
		  	$menu .= '<a href="../'.$folder.'/index.php?id='.$rs->Fields("id").'&lang='.$lang.'">'.$rs->Fields("titolo").'</a>';
		    $rs->MoveNext();	
		  }
		  $rs->Close();
		}
		return $menu;
	}
	
	  function getKeyIndexMenu($lang)  {
		global $conn,$_CONFIG;
		$qry = "SELECT provincia FROM province ORDER BY provincia;";
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $menu = '<div class="mdx">
		  			<a href="javascript:openIndex();">Keywords Index</a>
					  <div id="indexmenu" style="visibility:hidden; display:none">';
		  while(!$rs->EOF)  {
		  	$folder = eregi_replace(" ","-",$rs->Fields("titolo"));
		  	$menu .= '<a href="http://'.$_CONFIG['domain'].'">'.$this->fields['titolo'].' a '.$rs->Fields("provincia").'</a>';
		    $rs->MoveNext();	
		  }
		  $menu .= '</div>
		  </div>';
		  $rs->Close();
		}
		return $menu;
	}
	
	  function getKeyBottom($lang)  {
		global $conn;
		$qry = "SELECT id,titolo FROM keywords WHERE lang='$lang' AND attivo='y';";
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $menu = '';
		  while(!$rs->EOF)  {
		  	$folder = eregi_replace(" ","-",$rs->Fields("titolo"));
		  	$menu .= '<li><a href="../'.$folder.'/index.php?id='.$rs->Fields("id").'&lang='.$lang.'">'.$rs->Fields("titolo").'</a></li>';
		    $rs->MoveNext();	
		  }
		  $rs->Close();
		}
		return $menu;
	}
	
	  function getKeyBottomHome($sezione)  {
		global $conn,$lang;
		switch($sezione)  {
		  case "piscine":
		    $folder = FOLDER_P;
		  break;
		  
		  case "giardino":
		    $folder = FOLDER_G;
		  break;
		  
		  case "casa":
		    $folder = FOLDER_C;
		  break;
		  
		  case "fitness":
		    $folder = FOLDER_F;
		  break;
		  
		  default:
		    $folder = FOLDER_ITA;
		  break;
		}
		$qry = "SELECT id,titolo FROM keywords WHERE sezione='$sezione' AND lang='ita' AND attivo='y';";
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $menu = '';
		  while(!$rs->EOF)  {
		  	$titolo = eregi_replace(" ","-",$rs->Fields("titolo"));
		  	$menu .= '<a class=credits href="../'.$folder.'/'.$titolo.'/index.php?id='.$rs->Fields("id").'&lang='.$lang.'">'.$rs->Fields("titolo").'</a>&nbsp;&nbsp;';
		    $rs->MoveNext();	
		  }
		  $rs->Close();
		}
		return $menu;
	}
	
	function getKeyBottomIndex($lang)  {
		global $conn;
		switch($lang)  {
		  case "ita":
		    $folder = FOLDER_ITA;
		  break;
		  
		  case "eng":
		    $folder = FOLDER_ENG;
		  break;
		  
		  case "fra":
		    $folder = FOLDER_FRA;
		  break;
		  
		  case "ger":
		    $folder = FOLDER_GER;
		  break;
		}
		$qry = "SELECT id,titolo FROM keywords WHERE lang='$lang' AND attivo='y';";
		$rs = $conn->Execute($qry);
		if($rs)  {
		  $menu = '';
		  $n = $rs->RecordCount();
		  $i = 1;
		  while(!$rs->EOF)  {
		  	$titolo = eregi_replace(" ","-",$rs->Fields("titolo"));
		  	$menu .= '<a class=credits href="'.$folder.'/'.$titolo.'/index.php?id='.$rs->Fields("id").'&lang='.$lang.'">'.$rs->Fields("titolo").'</a>&nbsp;&nbsp;';
		    $rs->MoveNext();	
		  }
		  $rs->Close();
		}
		return $menu;
	}
}
?>